<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="姓名" prop="name">
      <el-input v-model="dataForm.name" placeholder="姓名"></el-input>
    </el-form-item>
    <el-form-item label="年龄" prop="age">
      <el-input v-model="dataForm.age" placeholder="年龄"></el-input>
    </el-form-item>
    <el-form-item label="性别" prop="sex">
      <el-input v-model="dataForm.sex" placeholder="性别"></el-input>
    </el-form-item>
    <el-form-item label="学历" prop="education">
      <el-input v-model="dataForm.education" placeholder="学历"></el-input>
    </el-form-item>
    <el-form-item label="籍贯" prop="nativePlace">
      <el-input v-model="dataForm.nativePlace" placeholder="籍贯"></el-input>
    </el-form-item>
    <el-form-item label="专业" prop="specialty">
      <el-input v-model="dataForm.specialty" placeholder="专业"></el-input>
    </el-form-item>
    <el-form-item label="联系方式" prop="tel">
      <el-input v-model="dataForm.tel" placeholder="联系方式"></el-input>
    </el-form-item>
    <el-form-item label="邮箱" prop="email">
      <el-input v-model="dataForm.email" placeholder="邮箱"></el-input>
    </el-form-item>
    <el-form-item label="工作年限" prop="experiences">
      <el-input v-model="dataForm.experiences" placeholder="工作年限"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          resumeId: 0,
          name: '',
          age: '',
          sex: '',
          education: '',
          nativePlace: '',
          specialty: '',
          tel: '',
          email: '',
          experiences: ''
        },
        dataRule: {
          name: [
            { required: true, message: '姓名不能为空', trigger: 'blur' }
          ],
          age: [
            { required: true, message: '年龄不能为空', trigger: 'blur' }
          ],
          sex: [
            { required: true, message: '性别不能为空', trigger: 'blur' }
          ],
          education: [
            { required: true, message: '学历不能为空', trigger: 'blur' }
          ],
          nativePlace: [
            { required: true, message: '籍贯不能为空', trigger: 'blur' }
          ],
          specialty: [
            { required: true, message: '专业不能为空', trigger: 'blur' }
          ],
          tel: [
            { required: true, message: '联系方式不能为空', trigger: 'blur' }
          ],
          email: [
            { required: true, message: '邮箱不能为空', trigger: 'blur' }
          ],
          experiences: [
            { required: true, message: '工作年限不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.resumeId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.resumeId) {
            this.$http({
              url: this.$http.adornUrl(`/generator/resumedata/info/${this.dataForm.resumeId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.name = data.resumeData.name
                this.dataForm.age = data.resumeData.age
                this.dataForm.sex = data.resumeData.sex
                this.dataForm.education = data.resumeData.education
                this.dataForm.nativePlace = data.resumeData.nativePlace
                this.dataForm.specialty = data.resumeData.specialty
                this.dataForm.tel = data.resumeData.tel
                this.dataForm.email = data.resumeData.email
                this.dataForm.experiences = data.resumeData.experiences
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/generator/resumedata/${!this.dataForm.resumeId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'resumeId': this.dataForm.resumeId || undefined,
                'name': this.dataForm.name,
                'age': this.dataForm.age,
                'sex': this.dataForm.sex,
                'education': this.dataForm.education,
                'nativePlace': this.dataForm.nativePlace,
                'specialty': this.dataForm.specialty,
                'tel': this.dataForm.tel,
                'email': this.dataForm.email,
                'experiences': this.dataForm.experiences
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
